#define AD9851_WCLK_SET HAL_GPIO_WritePin(GPIOF, GPIO_PIN_9, GPIO_PIN_SET) // WCLK
#define AD9851_WCLK_CLR HAL_GPIO_WritePin(GPIOF, GPIO_PIN_9, GPIO_PIN_RESET)

#define AD9851_FQUD_SET HAL_GPIO_WritePin(GPIOF, GPIO_PIN_8, GPIO_PIN_SET) // FreqUp
#define AD9851_FQUD_CLR HAL_GPIO_WritePin(GPIOF, GPIO_PIN_8, GPIO_PIN_RESET)

#define AD9851_DATA_Write_1 HAL_GPIO_WritePin(GPIOF, GPIO_PIN_2, GPIO_PIN_SET) // D7
#define AD9851_DATA_Write_0 HAL_GPIO_WritePin(GPIOF, GPIO_PIN_2, GPIO_PIN_RESET)

#define AD9851_RST_SET HAL_GPIO_WritePin(GPIOG, GPIO_PIN_1, GPIO_PIN_SET) // Reset(可有可无)
#define AD9851_RST_CLR HAL_GPIO_WritePin(GPIOG, GPIO_PIN_1, GPIO_PIN_RESET)

/********************************************
函数名称：init_dds
功    能：复位AD9851(串行)
参    数：无
返回值  ：无
*********************************************/
void Init_Dds_serial(void)
{
    AD9851_WCLK_CLR;
    AD9851_FQUD_CLR;
    HAL_Delay(2);
    AD9851_WCLK_SET;
    HAL_Delay(2);
    AD9851_WCLK_CLR;
    HAL_Delay(5);
    AD9851_FQUD_SET;
    HAL_Delay(2);
    AD9851_FQUD_CLR;
}

void AD985x_wr_serial(double dds, uint8_t phase, uint8_t mul)
{
    uint8_t i, w;
    uint32_t y;
    y = dds;

    // 写w4数据
    w = (y >>= 0);
    for (i = 0; i < 8; i++)
    {
        (w >> i) & 0x01 ? AD9851_DATA_Write_1 : AD9851_DATA_Write_0;
        AD9851_WCLK_SET;
        AD9851_WCLK_CLR;
    }
    // 写w3数据
    w = (y >> 8);
    for (i = 0; i < 8; i++)
    {
        (w >> i) & 0x01 ? AD9851_DATA_Write_1 : AD9851_DATA_Write_0;
        AD9851_WCLK_SET;
        AD9851_WCLK_CLR;
    }
    // 写w2数据
    w = (y >> 16);
    for (i = 0; i < 8; i++)
    {
        (w >> i) & 0x01 ? AD9851_DATA_Write_1 : AD9851_DATA_Write_0;
        AD9851_WCLK_SET;
        AD9851_WCLK_CLR;
    }
    // 写w1数据
    w = (y >> 24);
    for (i = 0; i < 8; i++)
    {
        (w >> i) & 0x01 ? AD9851_DATA_Write_1 : AD9851_DATA_Write_0;
        AD9851_WCLK_SET;
        AD9851_WCLK_CLR;
    }
    // 写w0数据
    w = (phase << 3) | mul;
    for (i = 0; i < 8; i++)
    {
        (w >> i) & 0x01 ? AD9851_DATA_Write_1 : AD9851_DATA_Write_0;
        AD9851_WCLK_SET;
        AD9851_WCLK_CLR;
    }
    // 移入始能
    AD9851_FQUD_SET;
    AD9851_FQUD_CLR;
}

/********************************************
函数名称：setPara_Serial
功    能：设置频率，相位，倍频
参    数：freq /Hz phase,phase:相位 0-31 ，一个控制字代表11.25°
                    mul:倍频 AD9850芯片不支持倍频
返回值  ：无
*********************************************/
void setPara_Serial(unsigned long freq, uint8_t phase, uint8_t mul)
{
    unsigned long dds;
    dds = 143.1655765333333 * freq;
    AD985x_wr_serial(dds, phase, mul);
}
